package com.ktjy.controller;

import com.ktjy.mapper.SupplierMapper;
import com.ktjy.pojo.Supplier;
import com.ktjy.service.SupplierService;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import util.Constants;
import util.PageSupport;

import java.util.List;


@Controller
@RequestMapping("/supplier")
public class SupplierController {
    private Logger logger=Logger.getLogger(SupplierController.class);

    @Autowired
    private SupplierService supplierService;

    //跳转到供应商列表
//    @RequestMapping("/list")
//    public String tolist() {
//        return "supplier/list";
//    }
//
    @RequestMapping("/list")
    public String getSupper(Model model,@RequestParam(value = "querySupName",required = false)String querySupName,
                            @RequestParam(value = "querySupCode",required = false)String querySupCode,
                            @RequestParam(defaultValue = "1")Integer pageIndex){
        List<Supplier> suppliers=null;
        try {
            //设置页面容量
            int pageSize = Constants.pageSize;
            //总数量
            int totalCount = supplierService.getUserCount(querySupName,querySupCode);
            //总页数
            PageSupport pages = new PageSupport();
            pages.setCurrentPageNo(pageIndex);
            pages.setPageSize(pageSize);
            pages.setTotalPageCount(totalCount);
            int totalPageCount = pages.getTotalPageCount();
            //控制首页和尾页
            if (pageIndex > totalPageCount) {
                pageIndex = totalPageCount;
                throw new RuntimeException("页码不正确");
            }
            suppliers=supplierService.getSupplierList(querySupName,querySupCode,pageIndex,pageSize);
            System.out.println(suppliers);
            model.addAttribute("suppliers",suppliers);
            model.addAttribute("totalCount",totalCount);
            model.addAttribute("totalPageCount",totalPageCount);
            model.addAttribute("currentPageNo",pageIndex);
            model.addAttribute("querySupName",querySupName);
            model.addAttribute("querySupCode",querySupCode);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取供应商列表失败");
        }
        return "supplier/list";
    }
}
